// 设置默认信息
$.validator.setDefaults({
    submitHandler: function (val) {
        oktipsoption = {
            type: 1,
            url: $('.formsub').attr('url'),
            msg: '提交成功'
        };
        ajaxform.init(oktipsoption, '.formsub');
        ajaxform.submit();
    },
    errorPlacement: function (error, element) {
        element.parents('.invoice_td').append(error);
        element.parents('.l_b').append(error);
    }
});

$(document).ready(function () {

    var transfereesubmit_form = $(".transfereesubmit");

    transfereesubmit_form.on('focus', '.l_b input', function () {
        $(this).addClass('u-ipt-active');
        $(this).removeClass('error');
        $(this).next('.error').hide();
    })

    transfereesubmit_form.on('blur', '.l_b input', function () {
        $(this).removeClass('u-ipt-active');
        if ($(this).parents('.l_b').find('label.error').is(":visible")) {
            $(this).addClass('error');
            $(this).parents('.l_b').find('.prompt').hide();
        } else {
            $(this).removeClass('error');
            $(this).removeClass('u-ipt-active');
            $(this).parents('.l_b').find('.prompt').show();
        }
    })

    // 单独选择更换背景
    $('.check_bg input').click(function () {
        if (this.checked) {
            $(this).prop('checked', true);
            $(this).parent('.check_bg').addClass('check_active');
        } else {
            $(this).prop('checked', false);
            $(this).parent('.check_bg').removeClass('check_active');
        }
    })

    // 点击选择合同跳出弹框
    $(".js_select").click(function () {
        layer.open({
            title: '<span class="title_s">选择合同</span>',
            type: 1,
            content: $(".layer_select"),
            area: '540px',
            btn: ['确定', '取消'],
            btnAlign: 'c',
            btn1: function () {
                $('.check_active').each(function () {
                    var That = $(this);
                    var id = That.children('input').prop('id');
                    var name_contract = That.parent('.list_info').find('.name_contract').text();
                    var ran = Math.random();
                    var c = '<div class="ht_info">'
                            + '<div class="ht_name fn-clear">'
                            + '<p id="' + id + '">' + name_contract + '</p>'
                            + '<i class="delete"></i>'
                            + '</div>'
                            + '<table class="receipt">'
                            + '<tr>'
                            + '<th>发票号</th>'
                            + '<th>应收账款金额</th>'
                            + '<th>单据日期</th>'
                            + '<th>账期（天）</th>'
                            + '</tr>'
                            + '<tr>'
                            + '<td>'
                            + '<div class="l_b fn-clear">'
                            + '<input type="text" name="bill_num[' + ran + ']" class="bill_num bill_num_f" placeholder="请输入发票号">'
                            + '</div>'
                            + '</td>'
                            + '<td>'
                            + '<div class="l_b fn-clear"> '
                            + '<input type="text" name="bill_balance_amt[' + ran + ']" class="bill_balance_amt bill_balance_amt_f" placeholder="请输入应收账款金额" >'
                            + '</div>'

                            + '</td>'
                            + '<td>'
                            + '<div class="l_b fn-clear"> '
                            + '<input type="text"  class="date_select bill_date bill_date_f" readonly name="bill_date[' + ran + ']" placeholder="请输入单据日期" >'
                            + '</div>'
                            + '</td>'
                            + '<td>'
                            + '<div class="l_b fn-clear">'
                            + '<input type="text" name="credit_period[' + ran + ']" class="credit_period credit_period_f" placeholder="请输入账期" >'
                            + '</div>'
                            + '<input type="hidden" name="contractid[' + ran + ']" value="' + id + '" id="' + id + '">'
                            + '<i class="icon_add"><img src="' + IMG_PATH + 'add.png"></i>'
                            + '</td>'
                            + '</tr>'
                            + '</table>'
                            + '</div>';

                    var r = $(".invoice_td").prepend(c);
                    That.children('input').prop('disabled', 'disabled');
                    That.addClass('check_already');
                    That.parent('.list_info').addClass('already');
                })
                layer.closeAll();
                $("#file_ht-error").remove();
            },
            btn2: function () {
                $(".check_bg").removeClass('check_active');
            },
            end: function () {
                $(".check_bg").removeClass('check_active');
            }
        });
    })

    // 点击合同上面的delete


    // 鼠标移上去切换背景
    $('.check_bg').mouseover(function () {
        $(this).addClass('hover');
    }).mouseleave(function () {
        $(this).removeClass('hover');
    })

    $(".list_info").mouseenter(function () {
        $(this).css("background", "#ebf5ff");
    }).mouseleave(function () {
        $(this).css("background", "#fff");
    })

    transfereesubmit_form.on("click", '.ht_name .delete', function () {
        $(this).parents('.ht_info').remove();
        var id = $(this).parents('.ht_info').find('p').prop('id');
        $(".list_info").each(function () {
            if ($(this).find('.check').prop('id') == id) {
                $(this).children('.check_bg').removeClass('check_already');
                $(this).find('.check').removeProp('disabled');
                $(this).removeClass('already');
            }
        });
    });

    transfereesubmit_form.on("click", '.icon_add', function () {
        var ran = Math.random();
        var id = $(this).parents('.ht_info').find('.ht_name').children('p').prop('id');
        var h = "<tr class='add_tr'>"
                + "<td>"
                + "<div class='l_b fn-clear'>"
                + "<input type='text' name='bill_num[" + ran + "]' class='bill_num bill_num_xh' placeholder='请输入发票号'>"
                + "</div>"
                + "</td>"
                + "<td>"
                + "<div class='l_b fn-clear'> "
                + "<input type='text' name='bill_balance_amt[" + ran + "]' class='bill_balance_amt bill_balance_amt_xh' placeholder='请输入应收账款金额' >"
                + "</div>"
                + "</td>"
                + "<td>"
                + "<div class='l_b fn-clear'> "
                + "<input type='text'  class='date_select bill_date bill_date_xh' readonly name='bill_date[" + ran + "]'  placeholder='请输入单据日期' > "
                + "</div> "
                + "</td>"
                + "<td>"
                + "<div class='l_b fn-clear'> "
                + "<input type='text' name='credit_period[" + ran + "]' class='credit_period credit_period_xh' placeholder='请输入账期' >"
                + "</div>"
                + '<input type="hidden" name="contractid[' + ran + ']" value="' + id + '" id="' + id + '">'
                + "<i class='icon_remove'>"
                + "<img src='" + IMG_PATH + "remove.png'/>"
                + "</i>"
                + "</td>"
                + "</tr>";
        $(this).parents(".receipt").append(h);
    })

    $("#transfereesubmit").submit(function (event) {
        $(".receipt .add_tr").each(function () {
            var bill_num_xh = $(this).find(".bill_num_xh");
            var bill_balance_amt_xh = $(this).find(".bill_balance_amt_xh");
            var bill_date_xh = $(this).find(".bill_date_xh");
            var credit_period_xh = $(this).find(".credit_period_xh");
            if (bill_num_xh.val() == '' && bill_balance_amt_xh.val() == '' && bill_date_xh.val() == '' && credit_period_xh.val() == '') {
                $(this).remove();
            }
            if (bill_num_xh.val() !== '' || bill_balance_amt_xh.val() !== '' || bill_date_xh.val() !== '' || credit_period_xh.val() !== '') {
                $('.bill_num_xh').each(function () {
                    $(this).rules('add', {
                        required: true,
                        messages: {required: "请填写发票号"}
                    })
                });
                $('.bill_balance_amt_xh').each(function () {
                    $(this).rules('add', {
                        required: true,
                        messages: {required: "请填写应收账款金额"}
                    })
                });
                $('.bill_date_xh').each(function () {
                    $(this).rules('add', {
                        required: true,
                        messages: {required: "请填写单据日期"}
                    })
                });
                $('.credit_period_xh').each(function () {
                    $(this).rules('add', {
                        required: true,
                        messages: {required: "请填写账期"}
                    })
                });

            }
        })
        $('.bill_num').each(function () {
            $(this).rules('add', {
                required: true,
                messages: {required: "请填写发票号"}
            })
        });
        $('.bill_balance_amt').each(function () {
            $(this).rules('add', {
                required: true,
                messages: {required: "请填写应收账款金额"}
            })
        });
        $('.bill_date').each(function () {
            $(this).rules('add', {
                required: true,
                messages: {required: "请填写单据日期"}
            })
        });
        $('.credit_period').each(function () {
            $(this).rules('add', {
                required: true,
                messages: {required: "请填写账期"}
            })
        });
    });

    transfereesubmit_form.on("focus", '.bill_date', function () {
        $(this).datepicker({
            dateFormat: 'yy-mm-dd'
        });
    })

    transfereesubmit_form.on("click", '.icon_remove', function () {
        $(this).parent('td').parent('tr').remove();
    });

    var record;
    transfereesubmit_form.on('keyup', '.credit_period', function () {
        var myThis = $(this);
        var val = myThis.val();
        var decimalReg = /^[0-9][0-9]*$/;
        if (val != '' && decimalReg.test(val)) {
            record = val;
        } else {
            if (val != '') {
                myThis.val(record);
            }
        }
        myThis.blur(function () {
            record = '';
        });
    });

    // 范围要求在大于等于0，小于等于9999999999.99之间
    transfereesubmit_form.on('keyup', '.bill_balance_amt', function () {
        var myThis = $(this);
        var val = myThis.val();
        var decimalReg = /^\d{0,999}\.{0,1}(\d{1,2})?$/;
        if (val != '' && decimalReg.test(val)) {
            record = val;
        } else {
            if (val != '') {
                myThis.val(record);
            }
        }
        myThis.blur(function () {
            record = '';
        });
    });
    

    $("#transfereesubmit").validate({
        rules: {
            file_ht:{
                 required: {
                    depends: function () {  
                        return ($(".ht_info").length <1);
                    }
                }
            }
        },
        messages: {
            file_ht:{
                required: '请选择合同',
            }
        },
        focusInvalid: false,
        onkeyup: false
    });
})



